﻿/// Author:					Walter Ferrari
/// Created:				2010-01-18
/// ref:                    http://www.snello.it
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.Generic;
using XLSXparsingLib;
using InteractChartCommonLib;
using DataFilesViewer.Controls;

namespace DataFilesViewer
{
    public static class Puppeteer
    {
        public static Dictionary<string, IFileDropFormat> droppedFiles ;

        public static void ShowData()
        {

        }

        public static void UpdateDataSectionPage(object sender, RoutedEventArgs e)
        {
            string rootItemSelected = sender as string;

            MainPage myPage = App.Current.RootVisual as MainPage;
            if (myPage == null)
                return;

            foreach (KeyValuePair<string, IFileDropFormat> keyValuePair in droppedFiles)
            {
                if (rootItemSelected == keyValuePair.Key)
                {
                    IFileDropFormat filedroppedObj = keyValuePair.Value;

                    myPage.dfvMainView.UpdateView(filedroppedObj, rootItemSelected);

                    /*  ORIGINAL WITH Datagrid at the bottom
                    foreach (string subitem in filedroppedObj.GetListSubItems())
                    {
                        if (subitem == selectedItem)  // NB TO DO: perorm comparison also with root tree node name
                        {
                            UpdateMainGrid(filedroppedObj, selectedItem);
                            //ShowChart(datasource);
                            return;
                        }
                    }
                     // endof original */
                }
            }

        }

        public static void ManageDrop(object sender, RoutedEventArgs e)
        {
            DragEventArgs dr = e as DragEventArgs;
            string objectName = GetSenderName(sender);

            if (dr.Data == null)
                return;

            IDataObject dataObject = dr.Data as IDataObject;
            FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[];

            foreach (FileInfo file in files)
            {
                // open Excel file
                if (file.Name.EndsWith("xlsx"))
                {
                    XLSXReader xlsxReader = new XLSXReader(file);

                    List<string> subItems = xlsxReader.GetListSubItems();
                    
                    if (droppedFiles == null)
                        droppedFiles = new Dictionary<string, IFileDropFormat>();
                    if (droppedFiles.ContainsKey(file.Name) == false)
                        droppedFiles.Add(file.Name, xlsxReader);
                }
            }
            MainPage myPage = App.Current.RootVisual as MainPage;
            foreach (KeyValuePair<string, IFileDropFormat> keyValuePair in droppedFiles)
            {
                if (myPage != null)
                {
                    myPage.dfvFilesList.AddItems(keyValuePair.Value.GetListSubItems(), keyValuePair.Key);
                }
            }
        }

        #region [ HELPERS ]


        private static string GetSenderName(object sender)
        {
            FrameworkElement fe = sender as FrameworkElement;
            return fe.Name;
        }
        #endregion
    }

}
